﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using AMS.Inventory.Entities;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;

namespace AMS.Inventory.DataAccess
{
    public class CoreDataAccess:BaseDataAccess
    {
        public void SavePlans(NameObjectDictionary nodPlanInfos, NameObjectDictionary nodPlanNameAssetInfos, int maxAssetAmount, ref StringBuilder sbErrMsg)
        {
            if (nodPlanInfos == null || nodPlanNameAssetInfos == null)
                return;
            sbErrMsg = new StringBuilder();
            foreach(string strPlanName in nodPlanInfos.Keys)
            {
                ArrayList alAssetInfos = ((ArrayList) nodPlanNameAssetInfos[strPlanName]);
                PlanInfo plan = ((PlanInfo) nodPlanInfos[strPlanName]);
                SavePlan(plan, alAssetInfos, maxAssetAmount, ref sbErrMsg);
            }
        }

        public void SavePlan(Guid planId, ArrayList assetInfos, int maxAssetAmount, ref StringBuilder sbErrMsg)
        {
            object objPlan = GetSingleObjectById(typeof (PlanInfo).Name, planId.ToString());
            if (objPlan == null || !(objPlan is PlanInfo))
                throw new ArgumentException("找不到盘点计划");
            PlanInfo plan = objPlan as PlanInfo;
            SavePlan(plan, assetInfos, maxAssetAmount, ref sbErrMsg);
        }

        public void SavePlan(PlanInfo plan,ArrayList assetInfos,int maxAssetAmount,ref StringBuilder sbErrMsg)
        {
            if (IsExists(plan))
                sbErrMsg.Append("【" + plan.Name + "】计划名称已经存在，不能保存\r\n");
            SaveOrUpdate(plan);
            Guid planId = plan.Id;
            int planExAmount = 0;
            for (int i = 0; i < assetInfos.Count; i++)
            {
                if (i>0 && i % maxAssetAmount == 0)
                {
                    planExAmount++;
                    PlanInfo newPlan = new PlanInfo();
                    newPlan.Name = plan.Name + "_EX_" + planExAmount;
                    if (IsExists(newPlan))
                    {
                        sbErrMsg.Append("【" + newPlan.Name + "】计划名称已经存在，不能保存\r\n");
                        continue;
                    }
                    newPlan.CreateDate = DateTime.Now;
                    newPlan.StartDate = plan.StartDate;
                    newPlan.FinishDate = plan.FinishDate;
                    newPlan.Memo = plan.Memo;
                    newPlan.Responsible = plan.Responsible;
                    newPlan.MaxAssetAmount = maxAssetAmount;
                    SaveOrUpdate(newPlan);
                    planId = newPlan.Id;
                }
                var ivObj = ((InventoryInfo)assetInfos[i]);
                ivObj.PlanId = planId;
                SaveOrUpdate(ivObj);
            }
        }

        public bool IsExists(PlanInfo objPlan)
        {
            IList ilObjs = Session.CreateQuery("Select count(*) from "+typeof(PlanInfo).Name+" o Where o.Id<>'"+objPlan.Id.ToString()+"' and o.Name='"+objPlan.Name+"'").List();
            if(ilObjs!=null && ilObjs.Count>0)
            {
                int amount = Convert.ToInt32(ilObjs[0]);
                if(amount>0)
                    return true;
            }
            return false;
        }
    }
}
